package com.cennavi.mineplay.mineplay_data.model;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.cennavi.mineplay.mineplay_data.init.InitTask;
import com.cennavi.mineplay.mineplay_data.model.DBConnectionImp.MysqlConnectionImpl;
import com.cennavi.mineplay.mineplay_data.model.DBConnectionImp.OracleConnectionImpl;
import com.cennavi.mineplay.mineplay_data.model.DBConnectionImp.PostgresConnectionImpl;
import com.cennavi.mineplay.mineplay_data.model.DBConnectionImp.ServersqlConnectionImpl;
import com.cennavi.mineplay.mineplay_data.model.db.inter.DBConnection;
import com.cennavi.mineplay.mineplay_data.model.transform.inter.Transform;

public class DBTaskAssember {

	private JSONObject json;
	private String destDBJdbc, dbDriverClass, username, password,
			destTableNames;

	public DBTaskAssember(JSONObject json, String destDBJdbc,
			String dbDriverClass, String username, String password,
			String destTableNames) {
		this.json = json;
		this.destDBJdbc = destDBJdbc;
		this.dbDriverClass = dbDriverClass;
		this.username = username;
		this.password = password;
		this.destTableNames = destTableNames;
	}

	public DBTask assember() {

		DBConnectionInfo dbinfo = new DBConnectionInfo();

		dbinfo.setCompany_id(json.getIntValue("company_id"));

		dbinfo.setDriverClass("");

		dbinfo.setDbType(json.getString("dbtype"));

		dbinfo.setJdbc(json.getString("jdbcurl"));

		dbinfo.setFrequency(json.getIntValue("frequency") * 2);

		dbinfo.setPassword(json.getString("password"));

		dbinfo.setSql(json.getString("sql"));

		dbinfo.setTimeName(json.getString("timeName"));

		dbinfo.setUpdateStyle(json.getIntValue("updateStyle"));

		dbinfo.setUsername(json.getString("username"));

		DbNameInfo dbnameinfo = new DbNameInfo();

		dbnameinfo.setDbname(json.getString("dbname"));

		dbnameinfo.setDbname_prefix(json.getString("dbname_prefix"));

		dbnameinfo.setIs_day(json.getIntValue("db_is_day"));

		dbnameinfo.setIs_dynamic(json.getIntValue("db_is_dynamic"));

		dbnameinfo.setIs_month(json.getIntValue("db_is_month"));

		dbnameinfo.setIs_month_split(json.getIntValue("db_is_month_split"));

		// 前端没有加入
		dbnameinfo
				.setMonth_prefix(json.getString("db_month_prefix") == null ? "P"
						: json.getString("db_month_prefix"));

		dbnameinfo.setNum_per_month(json.getIntValue("num_per_month"));

		TableNameInfo tablenameinfo = new TableNameInfo();

		tablenameinfo.setIs_one_table(json.getIntValue("is_one_table"));

		tablenameinfo.setTableName(json.getString("tableName"));

		tablenameinfo.setTableName_prefix(json.getString("tableName_prefix"));

		DBConnection dbconn = null;

		if ("oracle".equals(dbinfo.getDbType())) {
			dbconn = new OracleConnectionImpl(dbnameinfo, dbinfo);
		} else if ("sqlserver".equals(dbinfo.getDbType())) {
			dbconn = new ServersqlConnectionImpl(dbnameinfo, dbinfo);
		} else if ("mysql".equals(dbinfo.getDbType())) {
			dbconn = new MysqlConnectionImpl(dbnameinfo, dbinfo);
		} else if ("postgresql".equals(dbinfo.getDbType())) {
			dbconn = new PostgresConnectionImpl(dbnameinfo, dbinfo);
		}
		
		String trans = json.getString("trans");
		
		String[] splitsTrans = trans.length()>0? trans.split(","): new String[]{};

		List<Transform> transList = new ArrayList<Transform>();
		
		for(String key : splitsTrans){
			String className = InitTask.transList.get(key);
			
			try {
				Transform tran = (Transform) Class.forName(className).newInstance();
				
				transList.add(tran);
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		
		
		DBTask task = new DBTask(dbconn, dbinfo, dbnameinfo, tablenameinfo,
				transList, destDBJdbc, dbDriverClass, username, password,
				destTableNames);
		return task;
	}

}
